package boot.mq.rabbitmq.pruducer.controller;

import boot.mq.rabbitmq.core.RabbitmqProcessEnum;
import boot.mq.rabbitmq.core.RabbitmqRoutingEnum;
import boot.mq.rabbitmq.core.RabbitmqTopicConstant;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @Author : JCccc
 * @CreateTime : 2019/9/3
 * @Description :
 **/
@RestController
public class SendMessageController {

    @Autowired
    RabbitTemplate rabbitTemplate;  //使用RabbitTemplate,这提供了接收/发送等等方法

    @GetMapping("/sendDirectMessage")
    public String sendDirectMessage() {
        for (int i=0;i<10;i++){
            int messageId = i;
            String messageData = "hello, I'm here: "+UUID.randomUUID();
            String createTime = LocalDateTime.now().toString();
            Map<String,Object> map=new HashMap<>();
            map.put("messageId",messageId);
            map.put("messageData",messageData);
            map.put("createTime",createTime);
            //将消息携带绑定键值：TestDirectRouting 发送到交换机TestDirectExchange
            rabbitTemplate.convertAndSend("TestDirectExchange", "TestDirectRouting", map);
        }

        return "ok";
    }

    @GetMapping("/sendTopicMessageMan")
    public String sendTopicMessageMan() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "message: MAN-"+messageId.substring(0,8);
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> manMap = new HashMap<>();
        manMap.put("messageId", messageId);
        manMap.put("messageData", messageData);
        manMap.put("createTime", createTime);
        rabbitTemplate.convertAndSend("topicExchange", "topic_man", manMap);
        System.out.println("send topic_man message: "+manMap);

        return messageData;
    }

    @GetMapping("/sendTopicMessageWoman")
    public String sendTopicMessageWoman() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "message: woman-"+messageId.substring(0,8);
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> womanMap = new HashMap<>();
        womanMap.put("messageId", messageId);
        womanMap.put("messageData", messageData);
        womanMap.put("createTime", createTime);
        rabbitTemplate.convertAndSend("topicExchange", "topic_woman", womanMap);
        System.out.println("send topic_woman message: "+womanMap);

        return messageData;
    }

    @GetMapping("/sendVacationProcessApply")
    public String sendVacationProcessApply() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "vacation apply: "+messageId.substring(0,8);
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        rabbitTemplate.convertAndSend(RabbitmqProcessEnum.Vacation_Process.getExchange(), RabbitmqRoutingEnum.Vacation_Process_Apply.getRoutingKey(), map);
        String msg=String.format("send topic: %s", RabbitmqTopicConstant.Vacation_Process_Apply);
        String msg2=String.format("send message: %s",map);
        System.out.println(msg);
        System.out.println(msg2);

        return messageData;
    }

    @GetMapping("/sendVacationProcessConfirm")
    public String sendVacationProcessConfirm() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "vacation Confirm: "+messageId.substring(0,8);
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        rabbitTemplate.convertAndSend(RabbitmqProcessEnum.Vacation_Process.getExchange(), RabbitmqRoutingEnum.Vacation_Process_Confirm.getRoutingKey(), map);
        String msg=String.format("send topic: %s", RabbitmqTopicConstant.Vacation_Process_Confirm);
        String msg2=String.format("send message: %s",map);
        System.out.println(msg);
        System.out.println(msg2);

        return messageData;
    }

    @GetMapping("/sendFanoutMessage")
    public String sendFanoutMessage() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "message: testFanoutMessage ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        rabbitTemplate.convertAndSend("fanoutExchange", null, map);
        return "ok";
    }

}
